package com.drools.controller;

import com.drools.entity.People;
import com.drools.utils.RuleEngine;
import com.drools.utils.RuleEngineImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by LANCHUNQIAN on 2016/10/18.
 */

@RestController
@RequestMapping("/drools")
public class DRLController {
    private static final Logger RUN_LOG = LoggerFactory.getLogger(DRLController.class);

    private RuleEngine ruleEngine = new RuleEngineImpl();

    @RequestMapping("/create")
    public ModelAndView drlCreate(String page, String psalary){
        RUN_LOG.info("age = {} salary = {}", page, psalary);

         String rule =  "\nrule \"Salary Increment\"\n" +
                        "\twhen \n" +
                        "\t\t$p : People (age > " + page + ");\n" +
                        "\tthen\n" +
                        "\t\tSystem.out.println($p.getName());\n" +
                        "\t\t$p.setSalary($p.getSalary() + " + Double.parseDouble(psalary) + ");\n" +
                        "\tend\n";
        RUN_LOG.info("rule {}", rule);
        ruleEngine.addRule(rule);
        return new ModelAndView("redirect:http://localhost:8080/drools/use");
    }

    @RequestMapping("/use")
    public String drlUse(){
        People people = new People("lcq",30,1234,2000);
        RUN_LOG.info("Before Data name: {}, salary: {}",people.getName(),people.getSalary());
        System.out.println();
        ruleEngine.initEngine();
        ruleEngine.executeRuleEngine(people);
        RUN_LOG.info("After Data name: {}, salary: {}",people.getName(),people.getSalary());
        return "ok";
    }

}
